package com.xyh.flowershop.strategy;

import com.xyh.flowershop.cache.RedisCache;
import com.xyh.flowershop.constant.CacheConstant;
import com.xyh.flowershop.service.LoginService;
import com.xyh.flowershop.service.UserService;
import com.xyh.flowershop.utils.JwtUtils;
import com.xyh.flowershop.vo.req.user.LoginUser;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 小约翰可汗
 * @className AbstractLogin
 * @createTime 2024/7/1412:28
 */
@Component
public abstract class AbstractLogin implements LoginService {

    // 设置登录策略集合
    public static Map<Integer, AbstractLogin> strategyMap = new ConcurrentHashMap<>();

    @Autowired
    protected RedisCache redisCache;

    @Autowired
    protected  UserService userService;


    @PostConstruct
    public void init() {
            strategyMap.put(getType(), this);
    }

//    @Override
//    public String login(LoginUser loginUser){
//
//        return "";
//    }

    protected String creatToken(String userId){
        String token = JwtUtils.createJWT(userId);
        redisCache.setCacheObject(CacheConstant.LOGIN_PREFIX+userId, token);
        return token;
    }

    protected abstract Integer getType();
}
